package com.minimalist.lindi.mapper;

import com.minimalist.common.entity.lindi.po.LindiPurchaseApplication;
import com.minimalist.common.entity.lindi.vo.purchaseapplication.LindiPurchaseApplicationQueryVO;
import com.mybatisflex.core.BaseMapper;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryChain;
import com.mybatisflex.core.query.QueryWrapper;

/**
 * 林地指标交易平台_指标购买申请表 映射层。
 *
 * @author 11987
 * @since 2025-07-01
 */
public interface LindiPurchaseApplicationMapper extends BaseMapper<LindiPurchaseApplication> {

    /**
     * 根据购买申请ID查询购买申请信息
     *
     * @param purchaseApplicationId 购买申请ID
     * @return 购买申请信息
     */
    default LindiPurchaseApplication selectPurchaseApplicationByPurchaseApplicationId(Long purchaseApplicationId){
        return selectOneByQuery(QueryChain.create().eq(LindiPurchaseApplication::getPurchaseApplicationId, purchaseApplicationId));
    }

    /**
     * 根据购买申请ID删除购买申请
     *
     * @param purchaseApplicationId 购买申请ID
     * @return 删除的记录数
     */
    default int deletePurchaseApplicationByPurchaseApplicationId(Long purchaseApplicationId){
        return deleteByQuery(QueryChain.create().eq(LindiPurchaseApplication::getPurchaseApplicationId, purchaseApplicationId));
    }

    /**
     * 分页查询购买申请列表
     *
     * @param queryVO 查询条件
     * @return 分页结果
     */
    default Page<LindiPurchaseApplication> selectPagePurchaseApplicationList(LindiPurchaseApplicationQueryVO queryVO){
        QueryWrapper queryWrapper = QueryWrapper.create();
        
        // 构建查询条件
        queryWrapper.like(LindiPurchaseApplication::getPurchaseApplicationCode, queryVO.getPurchaseApplicationCode())
                .like(LindiPurchaseApplication::getPurchaseApplicationName, queryVO.getPurchaseApplicationName())
                .eq(LindiPurchaseApplication::getPurchaseApplicationStatus, queryVO.getPurchaseApplicationStatus())
                .ge(LindiPurchaseApplication::getApplicationTime, queryVO.getApplicationTimeStart())
                .le(LindiPurchaseApplication::getApplicationTime, queryVO.getApplicationTimeEnd())
                .orderBy(LindiPurchaseApplication::getCreateTime, false);
        
        return paginate(queryVO.getPageNum(), queryVO.getPageSize(), queryWrapper);
    }

}
